﻿
using System;
using System.Collections.Generic;
using System.Text;

using YouHP.Utility;
using YouHP.BLL;
using YouHP.Model;
using YouHP.Model.Group;

namespace YouHP.Pages
{
    public class Treatment : BasePage
    {

        protected override void ShowPage()
        {
            base.ShowPage();
            if (ErrorCount > 0) return;

            HandleCommon();
            if (ErrorCount > 0) return;

            if (IsPost) HandlePost();
            else HandleGet();
        }

        // 如果用户已经登录，记录当前用户的患病信息
        protected PatientRecordInfo prData;
        protected bool userUseCurrentTreatment = false;

        protected TreatmentInfo TreatmentData;
        protected List<DiseaseInfo> DiseaseDataListRelate;
        protected List<UserInfo> PatientDataListRelate;
        protected List<TopicInfo> TopicDataListHottest;
        protected List<TopicInfo> TopicDataListLatest;

        protected void HandleCommon()
        {

        }

        protected void HandlePost()
        {

        }

        protected void HandleGet()
        {
            int tid = RequestTools.GetQueryInt("id", 0);
            if (tid == 0)
            {
                this.AddErrLine("找不到治疗信息");
                return;
            }

            TreatmentData = TreatmentBLL.Instance.GetTreatmentInfo(tid);
            
            if (TreatmentData.Id <= 0)
            {
                this.AddErrLine("找不到治疗信息");
                return;
            }
            this.AddLocation("治疗列表", "/TreatmentAll.aspx");
            this.AddLocation(TreatmentData.Name, "/Treatment.aspx?id=" + TreatmentData.Id);
            this.SetTitle(TreatmentData.Name);

            int n = 8;
            DiseaseDataListRelate = DiseaseBLL.Instance.GetHottestDiseaseByTreatment(tid,n);

            if (UserData.Id >0)
            {
                PatientDataListRelate = PatientBLL.Instance.GetHottestPatientByTreatment(tid, n);
                TopicDataListHottest = TopicBLL.Instance.GetHottestTopic(n);
            }
            else
            {
                PatientDataListRelate = PatientBLL.Instance.GetHottestPatientByTreatment(tid, 2);
                TopicDataListHottest = TopicBLL.Instance.GetHottestTopic(3);
            }

            TopicDataListLatest = TopicBLL.Instance.GetLastestTopic(n);

            // 用户已经登录
            if (UserData.Id > 0)
            {
                prData = PatientBLL.Instance.GetPatientRecordInfo(UserData.Id);

                if (prData.Id > 0 && prData.DiseaseId > 0)
                {
                    userUseCurrentTreatment = GetTreatmentCurrentUsedByUser();
                }
            }
        }

        public bool GetTreatmentCurrentUsedByUser()
        {
            List<TreatmentHistoryInfo> UsingTreatmentList;
            UsingTreatmentList = PatientTreatmentBLL.Instance.GetAllCurUsedTreatments(prData.Id);

            if (UsingTreatmentList.Count <= 0)
                return false;

            foreach (TreatmentHistoryInfo treatmentHistoryInfo in UsingTreatmentList)
            {
                if (treatmentHistoryInfo.TreatmentId == TreatmentData.Id)
                {
                    return true;
                }
            }

            return false;
        }
    }
}